package com.xy.smartsms.service.carrierparam;

import android.content.Context;
import android.text.TextUtils;

import cn.com.xy.sms.sdk.util.DuoquUtils;

/**
 * Created by kangwenbo on 2017/3/15.
 */

public class UpdateRunnableManager {
    private static UpdateRunnableManager mInstance;
    private final long DEFAULT_TIME = 60000;
    private long mMinTimePeriod = DEFAULT_TIME;// 最小检测更新周期 单位：ms
    private long mLastUpdateTime;
    private String mIccid;
    private Context mContext;

    private UpdateRunnableManager(Context context) {
        mContext = context;
    }

    public static UpdateRunnableManager getInstance(Context context) {
        synchronized (UpdateRunnableManager.class) {
            if (mInstance == null) {
                mInstance = new UpdateRunnableManager(context);
            }
        }
        return mInstance;
    }

    public void checkUpdate(String centerNumber) {
        if (mLastUpdateTime + mMinTimePeriod < System.currentTimeMillis()) {
            mLastUpdateTime = System.currentTimeMillis();
            if (TextUtils.isEmpty(mIccid)) {
                //目前还没有获取对卡槽的规则设置，所以默认一个
                mIccid = DuoquUtils.getSdkDoAction().getIccidBySimIndex(-1);
            }
            new UpdateWhiteListRunnable().requestData(mContext, centerNumber, mIccid);
            new UpdateOnlineConfigRunnable().requestData(mContext, "1", centerNumber, mIccid);
            new UpdateBlackListRunnable().requestData(mContext, centerNumber,  mIccid);
        }
    }

    public void updateMinTime(long time) {
        if (time != 0) {
            if (mMinTimePeriod == DEFAULT_TIME && time > DEFAULT_TIME) {
                mMinTimePeriod = time;
            } else if (time > DEFAULT_TIME && time < mMinTimePeriod)
                mMinTimePeriod = time;
        }
    }
}
